<?php
require_once "../utils/config.php";
// The following file is not referenced, it merely contains functions that are proof of concept or have not yet been implemented / connected to the user interface
// Some functions have been used, but are in this file for easy reference for a future programmer group
// Good luck and have fun dealing with Facebook, They hate developers!


// The following code will delete all notifications for a user
function deleteUserNotifications() {
   $app_id = APP_ID;
   $app_secret = APP_SECRET;

   // The following code will grant an access_token needed to make queries.
   $token_url = "https://graph.facebook.com/oauth/access_token?" .
     "client_id=" . $app_id .
     "&client_secret=" . $app_secret .
     "&grant_type=client_credentials";

   $access_token = file_get_contents($token_url);
   
   print_r($access_token); // print thr access token

   $signed_request = $_REQUEST["signed_request"]; 
   list($encoded_sig, $payload) = explode('.', $signed_request, 2);
   $data = json_decode(base64_decode(strtr($payload, '-_', '+/')), true);
  // $user_id = $data["user_id"];
     $user_id = $facebook_id;

   //Get all app requests for user
   $request_url ="https://graph.facebook.com/" .
     $user_id .
     "/apprequests?" .
     $access_token;
   $requests = file_get_contents($request_url);

   //Print all outstanding app requests
   echo '<pre>';
   print_r($requests);
   echo '</pre>';

   //Process and delete app requests
   $data = json_decode($requests);
   foreach($data->data as $item) {
    $id = $item->id;
    $delete_url = "https://graph.facebook.com/" .
    $id . "?" . $access_token;

    $delete_url = $delete_url . "&method=delete";
    $result = file_get_contents($delete_url);
    echo("Was the request deleted? " . $result);
   }
} // end deleteUserNotifications
 

// JAVASCRIPT FUNCTIONS
?>


<script type="text/javascript" src=<?php echo LIB_JQUERY; ?>></script>
		<script type="text/javascript">


// Will send a notification to the user whose facebook ID matches the one passed in the parameter
function sendNotification(friendFbID) {
	FB.ui({method: 'apprequests', to: friendFbID, message: 'Looking to trade', data: 'tracking information for the user'});
}

// This function will query the facebook database returning all friends who have the application installed. The loop can be utilized for example adding the profile picture
// of every friend to a list
function getFriends() {
				var query = FB.Data.query('SELECT uid, name, pic_square FROM user WHERE uid IN (SELECT uid2 FROM friend WHERE uid1 = {0}) AND is_app_user = 1',
				                           <? echo $facebook_id; ?>);
				query.wait(function(rows) { // query.wait will pass an indexed array of the results of the query
					$('.friend').remove();
					for(var i = 0; i < rows.length; i++) {
						$('.InsertClassContainingList').append('<li class="friend">'+ "<a href = 'javascript:sendNotification(" + rows[i].uid + ")'>" + rows[i].name + '' + '<img src="' +  rows[i].pic_square + '" + id =>' + '</li>'); // Removes the friend class elements and adds the li elements to the friend_list class 
					} // done to avoid duplicates. 	This will also return the friend list with a clickable link for each name to send that friend a notification			
				});
		} // end getFriends()
		
// This function will pull up a request dialog filtered by friends that do not have the app installed
// Users can then select the friends they want to invite to use the app
function inviteToApp() {
FB.ui({ method: 'apprequests', filters: ['app_non_users'], 
       message: 'You should try this app out'});
 }
 
 // Use this code to initialize facebook javascript sdk
function initializeFacebook() {
<div id="fb-root"></div>
		<script>
			// asynchronously loads Facebook Javascript SDK, then displays card/friends list.
			window.fbAsyncInit = function() {
				FB.init({
					appId  : <?=APP_ID;?>, // currently our FB app ID
					status : true, // check login status
					cookie : true, // enable cookies to allow the server to access the session
					xfbml  : true  // parse XFBML
				});
			};
			
			(function() {
				var e = document.createElement('script');
				e.src = document.location.protocol + '//connect.facebook.net/en_US/all.js';
				e.async = true;
				document.getElementById('fb-root').appendChild(e);
			}());
}

// This function will pass a filter to the getCards.php file so that it will return the cards you want
// The cards will then be displayed using quicksand, a jquery plugin used for displaying cards
// The updateCardClickBinding() function will bind click listeners to each card
function filterAndDisplayCards(user_id, type) {
				$.post(
					'../ajax/getCards.php',
					{
						fb_id       : user_id,
						type_filter : type
					},
					function(data) {
						$('.cards').quicksand(
							$(data).children('.card'),
							{
								duration  : 700,
								attribute : 'id',
								easing    : 'easeInOutQuad'
							},
							function() {
								updateCardClickBinding();
							}
						);
					}
				);
			}

</script> 

